//加载会员权利信息

$(function () {
    $("#myCarousel").carousel({interval:5000});

    $("#jqGrid").jqGrid({
        url: baseURL + '/auth/modeInfo',
        datatype: "json",
        colModel: [
            { label: '会员等级', name: 'name', width: 80 },
            { label: '免费绑定店铺数量', name: 'storeNum', width: 80 ,formatter: function(value, options, row){
                    if(value<0){
                        return '<span class="label label-success">无限制</span>';
                    }else{
                        return value+'个';
                    }
                }},
            { label: '同时发布宝贝数', name: 'productNum', width: 80 ,formatter: function(value, options, row){
                    if(value<0){
                        return '<span class="label label-success">无限制</span>';
                    }else{
                        return value+'个';
                    }
                }},
            { label: '试用活动置顶', name: 'topPrice', width: 80 ,formatter: function(value, options, row){
                    if(value===0){
                        return '<span class="label label-success">免费</span>';
                    }else{
                        return value+'元/次';
                    }
                }},
            { label: '待返款费率', name: 'rate', width: 80,formatter: function(value, options, row){
                    if(value===0){
                        return '<span class="label label-success">无</span>';
                    }else{
                        return value*100+'%';
                    }
                } },
            { label: '免费试用价格', name: 'brokerage', width: 80 ,formatter: function(value, options, row){
                    if(value===0){
                        return '<span class="label label-success">免费</span>';
                    }else{
                        return value;
                    }
                }},
            { label: '下单搜索关键词', name: 'keyNum', width: 80 },
            { label: '活动优先审核', name: 'priority', width: 80,formatter: function(value, options, row){
                    if(value){
                        return '<span class="label label-success">是</span>';
                    }else{
                        return '<span class="label label-default">否</span>';
                    }
                }},
            { label: '精准人群标签', name: 'hasTag', width: 80 ,formatter: function(value, options, row){
                    if(value){
                        return '<span class="label label-success">是</span>';
                    }else{
                        return '<span class="label label-default">否</span>';
                    }
                }},
            { label: '全年淘客推广', name: 'tuiguang', width: 80 ,formatter: function(value, options, row){
                    if(value){
                        return '<span class="label label-success">是</span>';
                    }else{
                        return '<span class="label label-default">否</span>';
                    }
                }},
            { label: '售后退款保障', name: 'tuiguang', width: 80,formatter: function(value, options, row){
                    if(value){
                        return '<span class="label label-success">是</span>';
                    }else{
                        return '<span class="label label-default">否</span>';
                    }
                } },
            { label: '活动价格', name: 'timeLimits', width: 160 ,formatter: function(value, options, row){
                    if(value.length==0){
                        return '----'
                    }else{
                        var str ='';
                        for(var i=0;i<value.length;i++){
                            var obj = value[i];
                            str +='<label>' +
                                '        <input type="radio" name="mymodel" value="'+row.name+','+obj['id']+','+obj['price']+','+obj['name']+'">' +obj['name']+'/'+obj['price']+
                                '    </label></br>'
                        }
                        return str;
                    }
                }}
        ],
        viewrecords: true,
        rowList : [10,30,50],
        rownumbers: true,
        height: 310,
        rownumWidth: 25,
        autowidth:true,
        jsonReader : {
            root: "data"
        },
        // multiselect: true,
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#main',
    data:{
        merchant:{
            store:[],
            level:'',
            nobail:0,//尚未缴纳保证金
            verifyOrder:0,
            vipTime:null,
            deadline:null
        },
        addStoreName:'',
        oldStoreId:'',
        oldStoreName:'',
        isUpdate:false,
        balance:0,
        noticList:null,
        payTypeId:null,
        payTypeList:[],
        addAccountForm:{
            type:"",
            code:"",
            realName:"",
            username:""
        }
    },
    methods: {
        upStoreName:function (id,name) {
            vm.isUpdate=true;
            vm.oldStoreId=id;
            vm.oldStoreName = name;
            $("#showModel").click();
        },
        addStore:function () {
            vm.isUpdate=false;
            vm.oldStoreId='';
            vm.oldStoreName = '';
            $("#showModel").click();
        },
        subStoreName:function () {
            if('' == this.addStoreName || null ==this.addStoreName  ){
                alert("请输入店铺名称！");
            }else{
                var url = "/merchant/addStore";
                var param= {
                    "name":this.addStoreName
                }
                if(vm.isUpdate){
                    param["id"] = this.oldStoreId;
                    url = "/merchant/upStore";
                }
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    dataType: "json",
                    async: true,
                    contentType:"application/json",
                    data:JSON.stringify(param),
                    success: function(r){
                        if (r.code===0) {
                            alert("恭喜您，操作成功！",function () {
                                window.location.reload();
                            });
                        }else{
                            alert(r.info,function () {
                                $("#closeStore").click();
                            });
                        }
                    }
                });
            }
        },
        loadData:function () {
            //获取个人信息
            $.ajax({
                type: "GET",
                url: baseURL + "/merchant/center",
                dataType: "json",
                async: true,
                contentType:"application/json",
                data:JSON.stringify(),
                success: function(r){
                    if (r.code===0) {
                        if(r.data.deadline>0){
                        	vm.merchant.deadline = parseDate(r.data.deadline);
                        }else{
                        	vm.merchant.deadline = "--";
                        }
                        vm.merchant.adminAduit =r.data.adminAduit;
                        vm.merchant.applyList = r.data.applyList;
                        vm.merchant.applyAduit=r.data.applyAduit;
                        vm.merchant.store=r.data.store;
                        vm.merchant.level = r.data.level;
                        vm.merchant.balance=r.data.balance;
                        vm.payTypeId=r.data.payTypeId;
                        vm.merchant.completeList = r.data.completeList;
                        vm.merchant.storeNum = r.data.store.length;
                    }else{
                        alert(r.info);
                    }
                }
            });
        },
        showMyVIP:function () {
            $("#showMyVIP").click();
        },
        showWithdraweModel:function(){
            //先判断账户列表
            if(vm.payTypeList.length==0){
                //弹出填写提现账户
                layer.open({
                    type: 1,
                    skin: 'layui-layer-molv',
                    title: "完善提现账号",
                    area: ['550px', '280px'],
                    shadeClose: false,
                    contentType:"application/json",
                    content: jQuery("#addPayType"),
                    btn: ['保存','取消'],
                    btn1: function (index) {
                        $.ajax({
                            type: "POST",
                            url: baseURL + "/coustomer/bindAccount",
                            dataType: "json",
                            contentType: "application/json",
                            data: JSON.stringify(vm.addAccountForm),
                            success: function(r){
                                if(r.code == 0){
                                    layer.close(index);
                                    layer.alert('绑定成功！', function(){
                                        $("#showWithdraweModel").click();
                                    });
                                }else{
                                    layer.alert(r.info);
                                }
                            }
                        });
                    }
                });
            }else{
                $("#showWithdraweModel").click();
            }
        },
        subUserMode:function () {
            var str = $("input:radio:checked").val();
            var paramArray = str.split(",");
            var param= {
                "type":paramArray[0],
                "timeLimit":paramArray[3],
                "sum":paramArray[2]
            };
            $.ajax({
                type: "POST",
                url: baseURL + "/merchant/buyMember",
                dataType: "json",
                async: true,
                contentType:"application/json",
                data:JSON.stringify(param),
                success: function(r){
                    if (r.code===0) {
                        alert(r.info,function () {
                            $("#closeMyVIP").click();
                        });
                    }else{
                        alert(r.info);
                    }
                }
            });
        },
        //提交充值
        subCharge:function () {
            $("#chargeClose").click();
        	alert("正在支付",function () {
                            $("#closeCharge").click();
                        });
        	window.open(baseURL + "/auth/charge?sum="+$("#sum").val());
        },
        //提现
        subWithdraw:function () {
            if(isNaN($("#withdrawSum").val()) || $("#withdrawSum").val()<5){
                layer.alert("提现金额不可以小于5元哦！")
            }else{
                var param= {
                    "payTypeId":vm.payTypeList[0].id,
                    "sum":$("#withdrawSum").val()
                };
                $("#withdrawClose").click();
                $.ajax({
                    type: "POST",
                    url: baseURL + "/coustomer/withdraw",
                    dataType: "json",
                    async: true,
                    contentType:"application/json",
                    data:JSON.stringify(param),
                    success: function(r){
                        if (r.code===0) {
                            alert(r.info,function () {
                                $("#closeWithdraw").click();
                            });
                        }else{
                            alert(r.info);
                        }
                }
                });
            }
        },
        getNotic:function () {
            $.ajax({
                type: "GET",
                url: baseURL + "/notice/list",
                dataType: "json",
                async: true,
                contentType:"application/json",
                data:"",
                success: function(r){
                    if (r.code===0) {
                        console.log(r);
                        vm.noticList=r.data.dataList;
                    }else{
                        alert(r.info);
                    }
                }
            });
        },
        bindAccount:function () {
            $.ajax({
                type: "GET",
                url: baseURL + "/coustomer/bindAccount",
                dataType: "json",
                async: true,
                contentType:"application/json",
                data:"",
                success: function(r){
                    if (r.code===0) {
                        vm.noticList=r.data.dataList;
                    }else{
                        alert(r.info);
                    }
                }
            });
        }
    },
    mounted:function () {
        this.loadData();
        this.getNotic();
        //查询用户的账
        //获取用户的体现账户
        $.ajax({
            type: "GET",
            url: baseURL + "/coustomer/account",
            dataType: "json",
            async: true,
            contentType:"application/json",
            data:"",
            success: function(r){
                vm.payTypeList = r.data;
            }
        });
    }
})